<?php


namespace app\admin\controller;


class Admin extends Common
{
    public function login(){
        if (session('admin_id')>0) {
            $this->redirect('Index/index');
        }

        if(request()->isPost()){
            $data=input('post.');
            if($data['admin_name']==''){
                $this->error('用户名不能为空');
            }
            if($data['password']==''){
                $this->error('密码不能为空');
            }
            if(!captcha_check($data['captcha'])){
                $this->error('验证码不对');
            };
            $rs=db('admin')->where('admin_name',$data['admin_name'])->find();
            if($rs){
                if(md5($data['password']) != $rs['password']){
                    $this->error('密码不正确');
                }else{
                    session('admin_name',$rs['admin_name']);
                    session('admin_id',$rs['admin_id']);
//                    $rs=db('admin')->where('id', $rs['id'])->update([
//                        'last_ip' => request()->ip(),
//                        'last_time' => time(),
//                        'logins' => ['exp','logins+1'],
//                    ]);
                    $this->redirect('Index/index');
                }
            }else{
                $this->error('找不到用户');
            }
        }
        return $this->fetch('login');
    }

    public function logout(){
        session(null);
        $this->success('退出成功！','Admin/login');
    }

}